package com.utc.cards.common.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Point;
import android.widget.RelativeLayout;

import com.utc.cards.common.view.listener.CardDragListener;
import com.utc.cards.common.view.listener.CardTouchListener;
import com.utc.cards.model.card.Card;

public class CardsContainer
{

    private List<CardView> _cardViews = new ArrayList<CardView>();

    private RelativeLayout _associatedLayout;
    private Context _context;

    public static int STARTING_X = 20;
    public static int CARD_DISTANCE = 80;

    private Point _screenDimention;

    public CardsContainer(RelativeLayout layout, Point screenDimention, List<Card> cards)
    {
        _associatedLayout = layout;
        _context = layout.getContext();

        for (Card card : cards)
        {
            CardView cv = new CardView(card, _context);
            cv.setOnTouchListener(new CardTouchListener());
            _cardViews.add(cv);
        }

        _screenDimention = screenDimention;

        _associatedLayout.setOnDragListener(new CardDragListener(this));

        paint();
    }

    // Use this method only one time
    private void paint()
    {
        resetLayout();

        int x = STARTING_X;

        for (CardView cardView : _cardViews)
        {
            // Set the position
            cardView.setX(x);
            cardView.setOnTouchListener(new CardTouchListener());
            x += CARD_DISTANCE;

            _associatedLayout.addView(cardView);
        }
    }

    private void resetLayout()
    {
        _associatedLayout.removeAllViews();
    }

    public void refresh()
    {
        // System.out.println("[REFRESH] " + _cardViews.size());

        int x = STARTING_X;

        _associatedLayout.invalidate(); // Refresh the layout

        for (CardView card : _cardViews)
        {
            // Set the position
            card.setX(x);
            card.bringToFront();
            x += CARD_DISTANCE;
        }
    }

    // Allow to switch the position of one card according to its coordinates (x)
    public void update(String cardName, float x)
    {
        // System.out.println("SelectedCard: " + cardName + " - Position: " +
        // x);

        CardView selectedCard = getCardByName(cardName);

        if (selectedCard != null)
        {
            for (int i = 0; i < _cardViews.size(); i++)
            {
                int position = STARTING_X + i * CARD_DISTANCE;

                int indexToRemplace = -1;

                if (x >= position && x < position + CardView.CARD_WIDTH)
                {
                    // System.out.println("Position: " + position + " - " +
                    // (position + Card.CARD_WIDTH));
                    indexToRemplace = i + 1;
                }
                else if (x >= 0 && x < STARTING_X)
                {
                    indexToRemplace = 0;
                }

                if (indexToRemplace >= 0)
                {
                    _cardViews.remove(selectedCard);

                    if (indexToRemplace > _cardViews.size())
                    {
                        _cardViews.add(selectedCard);
                    }
                    else
                    {
                        _cardViews.add(indexToRemplace, selectedCard);
                    }

                    refresh();

                    return;
                }
            }
        }
    }

    private CardView getCardByID(int cardID)
    {
        for (CardView c : _cardViews)
        {
            if (c.getCard().getResourceId() == cardID)
                return c;
        }

        return null;
    }

    // Return a card from the card list according to its name
    public CardView getCardByName(String cardName)
    {
        for (CardView c : _cardViews)
        {
            if (c.getCard().getName().equals(cardName))
                return c;
        }

        return null;
    }

    public void addCard(Card c)
    {
        CardView cardView = new CardView(c, _context);
        cardView.setOnTouchListener(new CardTouchListener());
        _cardViews.add(cardView);

        float lastX = _associatedLayout.getChildAt(_associatedLayout.getChildCount() - 1).getX();

        cardView.setX(lastX + CARD_DISTANCE);
        _associatedLayout.addView(cardView);

        refresh();
    }

    private void removeCard(CardView c)
    {
        _associatedLayout.removeView(c);
        _cardViews.remove(c);

        refresh();
    }

    public void removeCard(Card c)
    {
        CardView cardView = getCardByName(c.getName());
        removeCard(cardView);
    }

    public void removeAllCards()
    {
        _cardViews.clear();

        resetLayout();

        refresh();
    }

    public void setCards(ArrayList<Card> cards)
    {
        _cardViews.clear();

        for (Card c : cards)
        {
            CardView cardView = new CardView(c, _context);
            _cardViews.add(cardView);
        }

        paint();
    }

    public List<CardView> getCards()
    {
        return _cardViews;
    }

    public RelativeLayout get_associatedLayout()
    {
        return _associatedLayout;
    }

    public void set_associatedLayout(RelativeLayout _associatedLayout)
    {
        this._associatedLayout = _associatedLayout;
    }

    public void centerCard(int cardID)
    {
        CardView c = getCardByID(cardID);

        if (c != null)
        {

            int middleIndex = (int) (_cardViews.size() / 2);

            _cardViews.remove(c);
            _cardViews.add(middleIndex, c);

            int x = STARTING_X;

            boolean hasBeenAdded = false;

            for (CardView card : _cardViews)
            {
                CARD_DISTANCE = (int) ((_screenDimention.x - 2 * CardsContainer.STARTING_X - (2 * 1.6 * CardView.CARD_WIDTH) - CardView.CARD_WIDTH) / _cardViews.size());
                if (card.equals(c) || hasBeenAdded)
                {
                    x += (int) (1.6 * CardView.CARD_WIDTH);
                    hasBeenAdded = !hasBeenAdded;
                }

                card.setX(x);

                x += CARD_DISTANCE;
            }
        }
    }

    public void centerCard(String cardName)
    {
        CardView c = getCardByName(cardName);

        if (c != null)
        {

            int middleIndex = (int) (_cardViews.size() / 2);

            _cardViews.remove(c);
            _cardViews.add(middleIndex, c);

            int x = STARTING_X;

            boolean hasBeenAdded = false;

            for (CardView card : _cardViews)
            {
                CARD_DISTANCE = (int) ((_screenDimention.x - 2 * CardsContainer.STARTING_X - (2 * 1.6 * CardView.CARD_WIDTH) - CardView.CARD_WIDTH) / _cardViews.size());
                if (card.equals(c) || hasBeenAdded)
                {
                    x += (int) (1.6 * CardView.CARD_WIDTH);
                    hasBeenAdded = !hasBeenAdded;
                }

                card.setX(x);

                x += CARD_DISTANCE;
            }
        }

    }
}
